<template>
  <div class="departments-container">
    <div class="app-container">
      <el-card v-loading="loading" class="tree-card">
        <!-- 用了一个行列布局 -->
        <tree-tools :node-data="company" :is-root="true" @add-depts="appDept" />

        <el-tree :data="list" :props="defaultProps" default-expand-all :expand-on-click-node="false">
          <template v-slot="{data}">
            <!--
              在父组件向子组件传值，绑定属性 属性名 单词与单词之间用 横线 '-' 隔开
              子组件接收变量名用小驼峰命名法命名
             -->
            <tree-tools
              :node-data="data"
              @del-depts="getDepartments"
              @add-depts="appDept"
              @edit-depts="editDept"
            />

          </template>
        </el-tree>
        <!-- 新增部门的弹框 -->
        <AddDepts
          ref="appDeptsRef"
          :show-dialog.sync="showDialog"
          :node-data="nodeData"
          :all-depts="allDepts"
          @close-dialog="showDialog=false"
          @add-success="getDepartments"
        />
      </el-card>

    </div>
  </div>
</template>

<script>
import { transList2Tree } from '@/utils'
import TreeTools from './components/tree-tools.vue'
import { reqGetDepartments } from '@/api/department'
import AddDepts from './components/add-depts.vue'
export default {
  name: 'Department',
  components: {
    TreeTools,
    AddDepts
  },
  data() {
    return {
      list: [],
      defaultProps: {
        children: 'children', // 可修改 data中的绑定的变量名
        label: 'name'
      },
      company: {
        name: '江苏传智播客教育科技股份有限公司',
        manager: '负责人'
      },
      showDialog: false,
      // showDialog: true
      nodeData: {},
      allDepts: [],
      loading: false
    }
  },
  created() {
    this.getDepartments()
  },
  methods: {
    // 获取组织架构的基本信息
    async getDepartments() {
      this.loading = true
      const { data: res } = await reqGetDepartments()
      // console.log(res)
      this.company = { name: res.companyName, manager: '负责人', id: '' }
      this.list = transList2Tree(res.depts, '')
      // this.list = res
      this.allDepts = res.depts
      this.loading = false
    },
    // handleClose() {
    //   this.showDialog = false
    // }
    // 新增部门
    appDept(nodeData) {
      this.showDialog = true
      this.nodeData = nodeData
    },
    // 编辑部门
    editDept(nodeData) {
      this.showDialog = true
      this.nodeData = nodeData
      // console.dir();
      this.$nextTick(() => {
        this.$refs.appDeptsRef.getDepartDetail()
      })
    }
  }
}
</script>

<style scoped lang="scss">
.tree-card {
  padding: 30px 30px;
  font-size:14px;
}

.el-tree {
  ::v-deep {
    // 小三角的样式, 去掉默认的小三角的旋转效果
    .el-tree-node__expand-icon.expanded {
      -webkit-transform: rotate(0deg);
      transform: rotate(0deg);
    }
    // 有子节点 且未展开 +
    .el-icon-caret-right:before {
      background: url("~@/assets/common/add-circle.png") no-repeat 0 0;
      content: '';
      display: block;
      width: 16px;
      height: 16px;
      font-size: 16px;
      background-size: 16px;
    }
    // 有子节点 且已展开 -
    .el-tree-node__expand-icon.expanded.el-icon-caret-right:before{
      background: url("~@/assets/common/minus-circle.png") no-repeat 0 0;
      content: '';
      display: block;
      width: 16px;
      height: 16px;
      font-size: 16px;
      background-size: 16px;
    }
    // 没有子节点
    .el-tree-node__expand-icon.is-leaf::before  {
      background: url("~@/assets/common/user-filling.png") no-repeat 0 0;
      content: '';
      display: block;
      width: 16px;
      height: 16px;
      font-size: 16px;
      background-size: 16px;
    }
  }
}
</style>
